﻿using System;
using System.Collections.Generic;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for OrderEditorView.xaml
    /// </summary>
    public partial class OrderEditorView
    {
        private readonly OrderController _orderControl;
        private readonly OrderStatusController _orderStatusControl;
        private readonly OrderDetailController _orderDetailControl;
        private readonly OrderDetailStatusController _orderDetailStatusControl;
        private readonly MemberController _memberControl;
        private readonly WarrantyController _warrantyControl;
        private readonly ComicController _comicControl;
        private readonly ComicStatusController _comicStatusControl;
        private readonly StaffController _staffControl;

        private readonly IList<OrderStatus> _orderStatuses;
        private readonly IList<OrderDetailStatus> _orderDetailStatuses;
        private readonly IList<Member> _members;
        private readonly IList<Warranty> _waranties;
        private readonly IList<ComicStatus> _comicStatuses;
        private IList<ComicData> _comicDatas;

        private readonly IList<ComicData> _comicOrders;
        private readonly Staff _staff;
        private DateTime _rentDate;
        private DateTime _returnDate;
        private int _total;

        public OrderEditorView(Staff staff)
        {
            InitializeComponent();
            Title = "Add Order";

            _orderControl = new OrderController();
            _orderStatusControl = new OrderStatusController();
            _orderDetailControl = new OrderDetailController();
            _orderDetailStatusControl = new OrderDetailStatusController();
            _memberControl = new MemberController();
            _warrantyControl = new WarrantyController();
            _comicControl = new ComicController();
            _comicStatusControl = new ComicStatusController();
            _staffControl = new StaffController();

            _orderStatuses = _orderStatusControl.GetAllOrderStatus();
            _orderDetailStatuses = _orderDetailStatusControl.GetAllOrderDetailStatus();
            _members = _memberControl.GetAllMember();
            _waranties = _warrantyControl.GetAllWarranty();
            _comicStatuses = _comicStatusControl.GetAllComicStatus();
            _comicDatas = _comicControl.GetAllComicDataAvailable();

            _comicOrders = new List<ComicData>();
            _staff = staff;
            _total = 0;

            textBoxStaff.Text = _staffControl.GetStaffName(_staff);
            textBoxTotal.Text = _total.ToString();

            comboBoxMember.ItemsSource = _memberControl.GetAllMemberUsername();
            comboBoxWarranty.ItemsSource = _warrantyControl.GetAllWarrantyType();
            comboBoxComic.ItemsSource = _comicControl.GetAllComicTitleAvailable();
        }

        private void ClearTextBox()
        {
            comboBoxMember.SelectedItem = null;
            comboBoxWarranty.SelectedItem = null;
            _total = 0;
            textBoxWarrantyNumber.Text = "";
            textBoxTotal.Text = _total.ToString();
            comboBoxComic.SelectedItem = null;

            _comicDatas = _comicControl.GetAllComicDataAvailable();
            _comicOrders.Clear();

            comboBoxComic.ItemsSource = _comicControl.GetAllComicTitleAvailable();
        }

        private void RefreshGrid()
        {
            dataGridOrder.ItemsSource = null;
            dataGridOrder.ItemsSource = _comicOrders;

            _total = 0;

            foreach (var comicOrder in _comicOrders)
            {
                _total += Int32.Parse(comicOrder.Rent);
            }
            
            textBoxTotal.Text = _total.ToString();
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (comboBoxMember.SelectedItem == null || comboBoxWarranty.SelectedItem == null || 
                textBoxWarrantyNumber.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else if (!_orderControl.IsValidDate(_rentDate, _returnDate))
            {
                MessageBox.Show("tanggal tidak valid");
            }
            else if (_comicOrders.Count == 0)
            {
                MessageBox.Show("order tidak boleh kosong");
            }
            else
            {
                var order = new Order
                                {
                                    Member = _members[comboBoxMember.SelectedIndex],
                                    Staff = _staff,
                                    RentDate = _rentDate,
                                    ReturnDate = _returnDate,
                                    Warranty = _waranties[comboBoxWarranty.SelectedIndex],
                                    WarrantyNumber = textBoxWarrantyNumber.Text,
                                    Total = _total,
                                    OrderStatus = _orderStatuses[0]
                                };

                _orderControl.AddOrder(order);

                foreach (var comicOrder in _comicOrders)
                {
                    var oldComic = _comicControl.GetComicById(comicOrder.Id);

                    var orderDetail = new OrderDetail
                                          {
                                              Order = order,
                                              Comic = oldComic,
                                              OrderDetailStatus = _orderDetailStatuses[0]
                                          };

                    _orderDetailControl.AddOrderDetail(orderDetail);

                    var newComic = new Comic
                                       {
                                           Id = oldComic.Id,
                                           Title = oldComic.Title,
                                           Author = oldComic.Author,
                                           Publisher = oldComic.Publisher,
                                           Year = oldComic.Year,
                                           Rent = oldComic.Rent,
                                           ComicStatus = _comicStatuses[1],
                                           Image = oldComic.Image
                                       };

                    _comicControl.EditComic(newComic);
                }

                MessageBox.Show("order sukses");
                ClearTextBox();
                RefreshGrid();
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void DatePickerRentDateSelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            _rentDate = Convert.ToDateTime(datePickerRentDate.SelectedDate.ToString());
        }

        private void DatePickerReturnDateSelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            _returnDate = Convert.ToDateTime(datePickerReturnDate.SelectedDate.ToString());
        }

        private void ButtonAddClick(object sender, RoutedEventArgs e)
        {
            if (comboBoxComic.SelectedItem == null)
            {
                MessageBox.Show("pilih comic dolo");
            }
            else
            {
                if (_comicOrders.Contains(_comicDatas[comboBoxComic.SelectedIndex]))
                {
                    MessageBox.Show("sudah dipilih");
                }
                else
                {
                    _comicOrders.Add(_comicDatas[comboBoxComic.SelectedIndex]);
                    RefreshGrid();
                }
            }
        }

        private void ButtonDeleteClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridOrder.SelectedItem == null)
                    throw new NullReferenceException();

                _comicOrders.RemoveAt(dataGridOrder.SelectedIndex);
                RefreshGrid();
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("pilih yang mau dihapus");
            }
        }

        private void ButtonClearClick(object sender, RoutedEventArgs e)
        {
            _comicOrders.Clear();
            RefreshGrid();
        }
    }
}
